package com.winning.gjhlht.ihe.hl7ClientService.data.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.framework.helper.PageHelper;
import com.framework.helper.ServerSQLExecutorUtils;
import com.framework.server.db.bean.PageBean;
import com.framework.server.db.bean.PageQueryBean;
import com.winning.common.Constans;
import com.winning.gjhlht.ihe.hl7ClientService.data.dao.IHEBaseDao;
import com.winning.gjhlht.ihe.hl7ClientService.data.dao.inter.IHEDataDaoInter;


@Repository(value = "com.winning.gjhlht.ihe.hl7ClientService.data.dao.impl.IHEDataDao")
public class IHEDataDao extends IHEBaseDao implements IHEDataDaoInter {
    private static Logger LOGGER = Logger.getLogger("pbsLoger");

    /**
     * 统计总数接口
     * 方法名称: queryTotalDataCount
     *
     * @param
     * @return int  返回类型说明<br/>
     * @throws <br/>
     * @author zq@winning.com.cn<br/>
     * 创建时间：2017-2-8/下午2:25:21<br/>
     */
    public int queryTotalDataCount(String sql, String columnName, String dbDataSourceID) {

        int result = -1;

        try {
            List<HashMap<String, Object>> resultMap = ServerSQLExecutorUtils.executeQuery(sql, dbDataSourceID);


            if (resultMap != null && resultMap.size() > 0) {

                Object objValue = resultMap.get(0).get(columnName);
                result = Integer.parseInt(objValue.toString());
            }

        } catch (Exception e) {
            //打印异常堆栈信息
            LOGGER.error(e, e.fillInStackTrace());

            e.printStackTrace();
        }

        return result;
    }


    /**
     * 执行SQL语句获取json数组对象
     * 方法名称: executeSQLForJSONArrayList
     *
     * @param
     * @return List<JSONObject>  返回类型说明<br/>
     * @throws <br/>
     * @author zq@winning.com.cn<br/>
     * 创建时间：2017-2-7/下午1:21:26<br/>
     */
    public List<JSONObject> executeSQLForJSONArrayList(String sql, Object[] params, String dbDataSourceID) {

        try {
            List<JSONObject> result = executeQueryReturnJSONListByDataSource(sql, params, dbDataSourceID);

            return result;

        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e, e.fillInStackTrace());
            return null;
        }
    }

    /**
     * 执行SQL语句，获取JSONObject对象
     * 方法名称: executSQLForJSONObject
     *
     * @param
     * @return JSONObject  返回类型说明<br/>
     * @throws <br/>
     * @author lenovo@winning.com.cn<br/>
     * 创建时间：2017-2-7/上午10:55:32<br/>
     */
    public JSONObject executSQLForJSONObject(String sql, Object[] params, String dbDataSourceID) {

        try {
            JSONObject result = executeQueryReturnJSONByDataSource(sql, params, dbDataSourceID);

            return result;

        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
            return null;
        }
    }


    /**
     * 执行sql语句获取结果
     * 方法名称: executeSQLByDataSourseID
     *
     * @param
     * @return List<HashMap   <   String   ,   Object>>  返回类型说明<br/>
     * @throws <br/>
     * @author zq@winning.com.cn<br/>
     * 创建时间：2017-1-13/上午9:02:46<br/>
     */
    public List<HashMap<String, Object>> executeSQLByDataSourseID(String sql, Object[] params, String dataSourseID) {
        try {
            List<HashMap<String, Object>> result = executeQueryReturnMapListByDataSource(sql, params, dataSourseID);

            return result;

        } catch (Exception e) {
            e.printStackTrace();

            LOGGER.error(e.getMessage());
            return null;
        }
    }

    /**
     * 分页查询
     * 方法名称: executeSQLByPage
     *
     * @param
     * @return PageQueryBean  返回类型说明<br/>
     * @throws <br/>
     * @author zhaqiang@winning.com.cn<br/>
     * 创建时间：2017-2-27/下午2:48:13<br/>
     */
    public PageQueryBean executeSQLByPage(PageBean pageBean, String basicSQL, String orderStr, String dataSourseID) {
        //按照分页从数据库中提取数据
        PageQueryBean pageQueryBean = null;
        try {

            //判断是否有OR
            if (dataSourseID.indexOf(Constans.DATA_SOURCE_ORACLE_TAG) > -1) {
                pageQueryBean = PageHelper.pageQuery(basicSQL, orderStr, pageBean, PageHelper.OraclePaging, dataSourseID, PageHelper.LowerXMLResultHandler);

            } else {
                pageQueryBean = PageHelper.pageQuery(basicSQL, orderStr, pageBean, PageHelper.SQLPaging, dataSourseID, PageHelper.LowerXMLResultHandler);
            }
        } catch (SQLException e) {
            //打印异常堆栈信息
            LOGGER.error(e, e.fillInStackTrace());

            e.printStackTrace();

            return null;
        }

        return pageQueryBean;
    }

    /**
     * 获取基本信息注册结果
     * 方法名称: getJBXXResgiterResult
     *
     * @param
     * @return List<JSONObject>  返回类型说明<br/>
     * @throws <br/>
     * @author zq@winning.com.cn<br/>
     * 创建时间：2017-1-7/下午1:46:47<br/>
     */
    public List<JSONObject> getJBXXResgiterResult(String sql, Object[] params, String dbDataSouseID) {
        try {
            List<JSONObject> result = executeQueryReturnJSONListByDataSource(sql, params, dbDataSouseID);
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e);
            return null;
        }
    }

    /**
     * 获取基本信息注册结果统计数据
     * 方法名称: getJBXXResgiterCount
     *
     * @param
     * @return JSONObject  返回类型说明<br/>
     * @throws <br/>
     * @author zq@winning.com.cn<br/>
     * 创建时间：2017-1-7/下午2:02:04<br/>
     */
    public JSONObject getJBXXResgiterCount(String sql, Object[] params, String dataSourseID) {
        try {
            JSONObject result = executeQueryReturnJSONByDataSource(sql, params, dataSourseID);

            return result;

        } catch (Exception e) {
            e.printStackTrace();

            LOGGER.error(e.getMessage());
            return null;
        }
    }

}
